package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsAgentProfitStatisMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsAgentProfitStatisMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.AgentProfitStatis;
import cc.rengu.igas.bsps.common.entity.BspsAgentProfitInfoBean;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.common.enums.RoleTypeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.AgentProfitInfoBean;
import cc.rengu.igas.bsps.facade.request.AgentMchntProfitInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.AgentMchntProfitInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 代理商分润信息查询
 * Created by 王成 on 2020/3/24
 */
public class AgentMchntProfitInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        AgentMchntProfitInfoQueryRequest agentMchntProfitInfoQueryRequest = new AgentMchntProfitInfoQueryRequest();
        ConvertUtil.convertOutput(agentMchntProfitInfoQueryRequest);
        return agentMchntProfitInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        AgentMchntProfitInfoQueryRequest agentMchntProfitInfoQueryRequest = (AgentMchntProfitInfoQueryRequest) request;
        AgentMchntProfitInfoQueryResponse agentMchntProfitInfoQueryResponse = new AgentMchntProfitInfoQueryResponse();
        BeanUtil.beanCopy(agentMchntProfitInfoQueryRequest, agentMchntProfitInfoQueryResponse);
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(agentMchntProfitInfoQueryRequest.getUserId()) || StringUtil.isEmptyOrNull(agentMchntProfitInfoQueryRequest.getAgentMchntNo()) ||
                StringUtil.isEmptyOrNull(agentMchntProfitInfoQueryRequest.getStartDate()) || StringUtil.isEmptyOrNull(agentMchntProfitInfoQueryRequest.getEndDate())) {
            rglog.error("用户编号<{}>,代理商编号<{}>,开始日期<{}>,截止日期<{}>不能为空!", agentMchntProfitInfoQueryRequest.getUserId(), agentMchntProfitInfoQueryRequest.getAgentMchntNo(), agentMchntProfitInfoQueryRequest.getStartDate(), agentMchntProfitInfoQueryRequest.getEndDate());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 获取用户角色，判断查询用户是否为代理商 */
        BspsMchntBindInfoMapper bspsMchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
        MchntBindInfo mchntBindInfo = bspsMchntBindInfoMapper.selectBspsMchntBindInfoListByUserIdAndMchntNo(agentMchntProfitInfoQueryRequest.getHeader().getInstId(), agentMchntProfitInfoQueryRequest.getUserId(), agentMchntProfitInfoQueryRequest.getAgentMchntNo());
        if (null == mchntBindInfo || (!mchntBindInfo.getBindUserRole().equals(RoleTypeEnum.MCHNT.getRoleType()) && !mchntBindInfo.getBindUserRole().equals(RoleTypeEnum.AGENT_MCHNT.getRoleType()) && !mchntBindInfo.getBindUserRole().equals(RoleTypeEnum.PLAT_MCHNT.getRoleType()))) {
            rglog.error("非法用户，不允许当前查询操作,userId:<{}>,agentMchntNo:<{}>", agentMchntProfitInfoQueryRequest.getUserId(), agentMchntProfitInfoQueryRequest.getAgentMchntNo());
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }
        /*查询交易数据*/
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(agentMchntProfitInfoQueryRequest.getPageIndex());
        pageInfo.setPageSize(agentMchntProfitInfoQueryRequest.getPageSize());
        BspsAgentProfitStatisMapper bspsAgentProfitStatisMapper = new BspsAgentProfitStatisMapperImpl();
        PageResult pageResult = bspsAgentProfitStatisMapper.selectBspsAgentProfitStatisInfoByAgentMchntNoPage(agentMchntProfitInfoQueryRequest.getHeader().getInstId(), agentMchntProfitInfoQueryRequest.getAgentMchntNo(), agentMchntProfitInfoQueryRequest.getStartDate(), agentMchntProfitInfoQueryRequest.getEndDate(), pageInfo);
        List<AgentProfitInfoBean> agentProfitInfosList = new ArrayList<>();
        if (pageResult != null) {
            for (Object object : pageResult.getResult()) {
                BspsAgentProfitInfoBean agentProfitInfos = (BspsAgentProfitInfoBean) object;
                AgentProfitInfoBean agentProfitInfoBean = new AgentProfitInfoBean();
                BeanUtil.beanCopy(agentProfitInfos, agentProfitInfoBean);
                agentProfitInfosList.add(agentProfitInfoBean);
            }
            agentMchntProfitInfoQueryResponse.setAgentProfitInfoList(agentProfitInfosList);
            agentMchntProfitInfoQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
            agentMchntProfitInfoQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
        } else {
            agentMchntProfitInfoQueryResponse.setTotalPage(0);
            agentMchntProfitInfoQueryResponse.setTotalRows(0);
        }

        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(agentMchntProfitInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        AgentMchntProfitInfoQueryResponse agentMchntProfitInfoQueryResponse = (AgentMchntProfitInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(agentMchntProfitInfoQueryResponse);
    }

    /**
     * 按清算日期统计代理商分润信息
     *
     * @param agentProfitStatis 代理商分润明细信息
     * @param agentProfitInfos  代理商分润统计信息
     */
    private void statisAgentProfitInfo(AgentProfitStatis agentProfitStatis, List<AgentProfitInfoBean> agentProfitInfos) {
        if (agentProfitInfos.isEmpty()) {
            AgentProfitInfoBean agentProfitInfo = new AgentProfitInfoBean();
            agentProfitInfo.setSettleDate(agentProfitStatis.getSettleDate());
            agentProfitInfo.setTransTotalAmt(agentProfitStatis.getTotalTransAmt());
            agentProfitInfo.setTransTotalNum(agentProfitStatis.getTotalTransNum());
            agentProfitInfo.setProfitTotalAmt(agentProfitStatis.getProfitFee());
            agentProfitInfos.add(agentProfitInfo);
        } else {
            Optional<AgentProfitInfoBean> agentProfitInfoOpt = agentProfitInfos.stream().filter(item -> item.getSettleDate().equals(agentProfitStatis.getSettleDate())).findFirst();
            if (agentProfitInfoOpt.isPresent()) {
                String totalTransAmt = AmountUtil.addition(agentProfitInfoOpt.get().getTransTotalAmt(), agentProfitStatis.getTotalTransAmt());
                int totalTransNum = Integer.parseInt(agentProfitInfoOpt.get().getTransTotalNum()) + Integer.parseInt(agentProfitStatis.getTotalTransNum());
                String profitFee = AmountUtil.addition(agentProfitInfoOpt.get().getProfitTotalAmt(), agentProfitStatis.getProfitFee());
                agentProfitInfoOpt.get().setTransTotalAmt(totalTransAmt);
                agentProfitInfoOpt.get().setTransTotalNum(String.valueOf(totalTransNum));
                agentProfitInfoOpt.get().setProfitTotalAmt(profitFee);
            } else {
                AgentProfitInfoBean agentProfitInfo = new AgentProfitInfoBean();
                agentProfitInfo.setSettleDate(agentProfitStatis.getSettleDate());
                agentProfitInfo.setTransTotalAmt(agentProfitStatis.getTotalTransAmt());
                agentProfitInfo.setTransTotalNum(agentProfitStatis.getTotalTransNum());
                agentProfitInfo.setProfitTotalAmt(agentProfitStatis.getProfitFee());
                agentProfitInfos.add(agentProfitInfo);
            }
        }
    }
}
